package edu.guet.server.mapper;

import edu.guet.server.model.Semester;
import edu.guet.server.model.SemesterStatistic.SemesterPermissions;
import edu.guet.server.model.SemesterStatistic.SemesterTask;
import edu.guet.server.model.SemesterStatistic.Student;
import edu.guet.server.model.Stu;
import edu.guet.server.model.dto.ShowSemesterDTO;
import edu.guet.server.model.dto.StuSemDTO;
import edu.guet.server.model.dto.StudentScoreDTO;
import edu.guet.server.model.vo.ExcellentCourseVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SemesterMapper {
    /**
     * 添加学期课程
     * @param semester
     */
    @Insert("INSERT INTO semester (course_id,semester_name,semester_code,start_time,end_time,tech_id) " +
            "values (#{course_id},#{semester_name},#{semester_code},#{start_time},#{end_time},#{tech_id})")
    @Options(useGeneratedKeys = true,keyProperty = "semester_id",keyColumn = "course_id")
    void addSemester(Semester semester);

    /**
     * 根据复制课程码查找semester
     * @param semester_code
     * @return
     */
//    @Select("SELECT * from semester where semester_code=#{semester_code}")
     Semester findSemesterCode(String semester_code);


    /**
     * 更新课程信息
     * @param semester
     */
    void updateSemester(Semester semester);

    /**
     * 根据课程id查找课程
     * @param semester_id
     */
    @Select("SELECT * from semester where semester_id=#{semester_id}")
    Semester findSemesterById(Integer semester_id);

    /**
     * 根据课程ids获取所有课程信息
     * @param semIds
     * @return
     */
    List<ShowSemesterDTO> findSemesterandReByIds(List<Integer> semIds,Integer semester_status);

    /**
     * 根据学生id查询相关课程id
     * @param userId
     * @return
     */
    @Select("select stu_semester.semester_id from stu_semester where user_id=#{userId}")
    List<Integer> findsemesterIdsByuserId(Integer userId);

    /**
     * 更新课堂状态（是否开放/截止
     * @param semesterId
     */
    @Update("update semester set semester_status=#{status} where course_id=#{semesterId}")
    void updateSemStatus(Integer semesterId,Integer status);

    /**
     * 查找stusem是否已经加课
     * @param stuSemDTO
     * @return
     */
    @Select("select * from stu_semester where semester_id=#{semester_id} and user_id=#{user_id}")
    StuSemDTO findStuSem(StuSemDTO stuSemDTO);

    /**
     * 学生加课
     * @param stuSemDTO
     */
    @Insert("INSERT stu_semester values (#{course_id},#{course_id},#{update_time})")
    void addStuSem(StuSemDTO stuSemDTO);

    /**
     * 刷新加课人数
     */
    @Update("UPDATE semester set stu_cnt=stu_cnt+1 where course_id=#{course_id}")
    void updateSemStuCnt(Integer semester_id);

    SemesterTask findSemesterTask(Integer semesterId);

    @Select("select semester.start_time from semester where semester_id=#{semesterId}")
    String findStarttimeById(Integer semesterId);

    @Select("SELECT user_id from stu_semester where semester_id=#{semesterId}")
    List<Integer> findSemStuIds(Integer semesterId);

    @Update("UPDATE stu_semester set aver_test=#{aveStuTest},aver_hmk=#{aveStuHmk} where user_id=#{stuId}")
    void updateAverPerUser(Double aveStuTest, Double aveStuHmk, Integer stuId);

    Double getAverageTest(Integer semesterId);

    Double getAverageHmk(Integer semesterId);
    @Update("UPDATE stu_semester set valid_socre=#{vailScore} where user_id=#{stuId}")
    void updateSemesterByVaild(Integer stuId, Double vailScore);

    @Update("update semester set ave_vaild_score=#{vailScore} where course_id=#{semesterId}")
    void updateSemesterByvaildScore(Double vailScore, Integer semesterId);

    @Update("update semester set hmkPermission=#{hmkPermission},testPermission=#{testPermission} where semester_id=#{semesterId}")
    void addPermissions(Double hmkPermission, Double testPermission,Integer semesterId);

    @Select("select hmkPermission,testPermission from semester where semester_id=#{semesterId}")
    SemesterPermissions findPermissionsById(Integer semesterId);

    Student getStuAndValid(Integer stuId);

    @Select("select * from semester where semester_code=#{semesterCode}")
    Semester findSemesterByCOde(String semesterCode);

    Integer getCourseIdBysemId(Integer semesterId);

    List<StudentScoreDTO> findStudentScores(Integer semesterId);

    List<Stu> getStuList(Integer semesterId);

    void deleteSemById(Integer semesterId);

    @Select("SELECT course_id FROM semester WHERE semester_id=#{semesterId}")
    Integer getCIdBysemId(Integer semesterId);

    boolean insertSemStus(List<Stu> stuList, Integer semesterId);
}
